package cn.sciento.farm.devicemanager.gateway.uplink.service.inverter;

import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import cn.sciento.farm.devicemanager.gateway.model.inverter.InverterRealData;
import cn.sciento.farm.devicemanager.gateway.model.inverter.InverterUplinkPacket;
import cn.sciento.farm.devicemanager.gateway.uplink.service.base.AbstractStringAdapter;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wumu
 */
@Slf4j
@Service(value = MqttConstant.PROTOCOL_PREFIX + MqttConstant.PROTOCOL_INVERTER)
public class InverterDataAdapter extends AbstractStringAdapter {

    @Autowired
    private List<InverterDisposeService> inverterDisposeServiceList;

    @Override
    public void analyseStringData(String[] subTopic, String message) {
        log.info("变频器topic {}  消息 {}",subTopic,message);
        if (subTopic.length == 3) {
            String type = subTopic[1];
            String eui = subTopic[2];
            switch (type) {
                case MqttConstant.UP:
                    InverterUplinkPacket uplinkPacket = JSON.parseObject(message, InverterUplinkPacket.class);
                    InverterRealData realData = new InverterRealData(uplinkPacket);
                    realData.setEui(eui);
                    for(InverterDisposeService service :inverterDisposeServiceList){
                        service.changeParams(realData);
                    }
                    break;
                case MqttConstant.OFFLINE:
                    for(InverterDisposeService service :inverterDisposeServiceList){
                        service.changeOnline(eui, MqttConstant.DEVICE_OFFLINE_CODE);
                    }
                default:
                    break;
            }

        }
    }
}
